package com.itheima.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

/**
 * @作者 itcast
 * @创建日期 2020/12/1 10:35
 **/
@RestController
@RequestMapping("consumer")
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("{id}")
    public Map findById(@PathVariable("id") String id){
        // RestTemplate 是Spring封装的Http客户端模板对象
        return restTemplate.getForObject("http://user-service/user/"+id,Map.class);
    }

//    @GetMapping("{id}")
//    public Map findById(@PathVariable("id") String id){
//        // 从注册中心 获取user-service的服务注册列表
//        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
//        String host = instances.get(0).getHost();
//        System.out.println("当前的host地址==>"+host);
//        int port = instances.get(0).getPort();
//        // 要调用用户微服务  获取用户信息
//        // RestTemplate 是Spring封装的Http客户端模板对象
//        return restTemplate.getForObject("http://"+host+":"+port+"/user/"+id,Map.class);
//    }
}
